using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace etbTOEIC
{
	/// <summary>
	/// Summary description for TestGenerate.
	/// </summary>
	public class TestGenerate
	{
		public TestGenerate()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		static public void TempTestGenerate(int []nNumberOfQuesDef, string []sWhereClauses)
		{
			DB2MainForm.TestID = 0;
			string strsql = "DELETE * FROM tblTempTest";
			MyDatabase.ExecuteNonQuery(strsql);
			
			int nOrder = 0;
			string []SQLs = new string[7];
			for (int k=0; k<7; k++)
			{
				int SID = k+1;
				if (nNumberOfQuesDef[k] <= 0)
					continue;

				if (SID==4 || SID==7)
					sWhereClauses[k] = sWhereClauses[k].Replace("BookID", "T.BookID");
				else
					sWhereClauses[k] = sWhereClauses[k].Replace("BookID", "Q.BookID");
				sWhereClauses[k] = sWhereClauses[k].Replace("UsedTime", "Q.UsedTime");
				
				SQLs[k] = " SELECT DISTINCT Q.QuestionID, Q.TopicID, T.SectionID ";
				SQLs[k] +=" FROM tblQuestion Q, tblTopic T ";
				SQLs[k] +=" WHERE Q.TopicID=T.TopicID AND T.SectionID="+SID.ToString();
				if (sWhereClauses[k]!=null && sWhereClauses[k].Length>0)	SQLs[k] +="	AND "+sWhereClauses[k];
				SQLs[k] +=" ORDER BY Q.TopicID";

				DataSet ds = new DataSet();
				MyDatabase.ExecuteGenQuery(SQLs[k], ref ds);
				TopicTestGenerate(ref ds, ref nNumberOfQuesDef[k], nOrder);
				ds.Clear();
				
				nOrder += nNumberOfQuesDef[k];
			}
		}
		static private void TopicTestGenerate(ref DataSet ds, ref int NumQues, int StartOrder)
		{
			long []QuestionIDs;
			Random randNumber = new Random();
			ArrayList arrQID = new ArrayList();
			ArrayList arrTID = new ArrayList();
			ArrayList storedQIDs = new ArrayList();
			ArrayList storedTIDs = new ArrayList();

			bool IsGroupTopic = false;
			if (NumQues<= 0)
				return;

			foreach(DataTable myTable in ds.Tables)
			{
				foreach(DataRow myRow in myTable.Rows)
				{
					arrQID.Add(myRow["QuestionID"].ToString());
					arrTID.Add(myRow["TopicID"].ToString());
					IsGroupTopic = (myRow["SectionID"].ToString()=="4" || myRow["SectionID"].ToString()=="7");
				}
			}
			NumQues = NumQues<arrQID.Count? NumQues : arrQID.Count;
			QuestionIDs = new long[NumQues];

			for (long k=0; k<NumQues; k++)
			{
				int CandIndex = randNumber.Next(arrQID.Count);
				
				long QID = Convert.ToInt64(arrQID[CandIndex]);
				long TID = Convert.ToInt64(arrTID[CandIndex]);
				storedQIDs.Add(QID);
				storedTIDs.Add(TID);
				arrQID.RemoveAt(CandIndex);
				arrTID.RemoveAt(CandIndex);
				
				
				if (IsGroupTopic)
				{
					int Prev = CandIndex - 1, Next = CandIndex;
					long QID2 = 0;
					while(Next < arrTID.Count && k<NumQues && Convert.ToInt64(arrTID[Next].ToString()) == TID)
					{
						QID2 = Convert.ToInt64(arrQID[Next].ToString());
						storedQIDs.Add(QID2);
						storedTIDs.Add(TID);
						arrQID.RemoveAt(Next);
						arrTID.RemoveAt(Next);
						k++;
					}
					while(Prev >= 0 && k<NumQues && Convert.ToInt64(arrTID[Prev].ToString()) == TID)
					{
						QID2 = Convert.ToInt64(arrQID[Prev].ToString());
						storedQIDs.Add(QID2);
						storedTIDs.Add(TID);
						arrQID.RemoveAt(Prev);
						arrTID.RemoveAt(Prev);
						k++;
						Prev--;
					}
					
				/*	for (int i=0; i<arrQID.Count && k<NumQues+1; i++)
						if (Convert.ToInt64(arrTID[i].ToString()) == TID)
						{
							long QID2 = Convert.ToInt64(arrQID[i].ToString());
				
							storedQIDs.Add(QID2);
							storedTIDs.Add(TID);
							arrQID.RemoveAt(i);
							arrTID.RemoveAt(i);
							
							k++;
							i--;
						}
				*/
					if(k >= NumQues)
					{
						//remove one question
						int nCount = 1;
						int i = 0;
						TID = Convert.ToInt64(storedTIDs[0].ToString());
						for(i = 1; i < storedTIDs.Count; i++)
						{
							if(Convert.ToInt64(storedTIDs[i].ToString()) == TID)
								nCount++;
							else
							{
								nCount = 1;
								TID = Convert.ToInt64(storedTIDs[i].ToString());
							}
							if(nCount > 2)
								break;
						}
						if(i < storedTIDs.Count)
						{
							storedQIDs.RemoveAt(i);
						}
					}
				}
			}

			for (int k=0; k<storedQIDs.Count; k++)
			{
				long QID = Convert.ToInt64(storedQIDs[k]);
				MyDatabase.InsertQuesToTmpTest(DB2MainForm.TestID,QID, StartOrder+k);
			}
		}
	}
}
